// export function clearNull(params) {
//   Object.keys(params).length !== 0 && Object.keys(params).forEach(ele => {
//     if (params[ele] === '' || params[ele] == null || params[ele].length === 0) {
//       delete params[ele]
//     } else if (typeof params[ele] === 'object' && Object.keys(params[ele]).length === 0) {
//       delete params[ele]
//     }
//   })
//   return params
// }
// function deepCopy(target) {
//   // 排除空值
//   if (typeof target === 'object' && target) {
//     const cloneTarget = Array.isArray(target) ? [] : {}
//     for (const key in target) {
//       cloneTarget[key] = deepCopy(target[key])
//     }
//     return cloneTarget
//   } else {
//     return target
//   }
// }

export function deepCopy(orgin, target) {
  const tar = target || {}
  const toStr = Object.prototype.toString
  for (const key in orgin) {
    if (Object.hasOwnProperty.call(orgin, key)) {
      if (typeof orgin[key] === 'object' && orgin[key] !== null) {
        tar[key] = toStr.call(orgin[key]) === '[object Array]' ? [] : {}
        deepCopy(orgin[key], tar[key])
      } else {
        tar[key] = orgin[key]
      }
    }
  }
  return tar
}

// 这个可能更好用
export function deepClone(origin, weakMap = new WeakMap()) {
  // 判断原始值
  if (typeof origin !== 'object' || !origin) return origin

  if (weakMap.get(origin)) return origin

  const traget = new origin.constructor()

  weakMap.set(origin, traget)

  for (const key in origin) {
    if (Object.hasOwnProperty.call(origin, key)) {
      traget[key] = deepClone(origin[key], weakMap)
    }
  }
  return traget
}

export function clearNull(params) {
  const typeList = new Set([
    '[object Null]',
    '[object Undefined]'
  ])
  const result = deepCopy(params)
  Object.keys(result).length !== 0 && Object.keys(result).forEach(ele => {
    // if (result[ele] === '' || result[ele] == null) {
    //   delete result[ele]
    // }
    // 为 0 直接false 不符合预期
    // if (!result[ele] && result[ele] !== 0) {
    //   delete result[ele]
    // }
    if (typeList.has(Object.prototype.toString.call(result[ele])) || result[ele] === '') {
      delete result[ele]
    }
  })
  return result
}
